package lk.mrt.cse.wihidum.listener.storage;

import org.apache.log4j.*;
import org.hibernate.*;
import org.hibernate.criterion.*;
import java.util.*;
/**
 * Created by IntelliJ IDEA.
 * User: Will
 * Date: Feb 26, 2010
 * Time: 10:52:23 AM
 * To change this template use File | Settings | File Templates.
 */
public class SubscriptionDAO
{
      Session session = null;
      private static final Logger logger = Logger.getLogger (SubscriptionDAO.class);

    /**
     * Default constructor
     */
    public SubscriptionDAO ()
    {
        this.session = DataBaseManager.getSessionFactory ().getCurrentSession ();
    }


    public boolean saveSubscriber (Subscription subscription)
    {
        try
        {
            Transaction tx = session.beginTransaction ();
            session.save (subscription);
            tx.commit ();
            logger.info ("Subscription saved successfully");
            return true;
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in saving subscriber", ex);
            return false;
        }
    }
   public Subscription getSubscriberDetails(String subscriptionID,String topic)
   {
       Subscription subscription=null;
       ArrayList<Subscription> retrievedSubscriptions;

       try
       {
          Transaction tx=session.beginTransaction();
          Criteria criteria=session.createCriteria(Subscription.class).add(Restrictions.eq("subscriptionID",subscriptionID)).add(Restrictions.eq("filter",topic));
          List<Subscription> subList=criteria.list();
          if(!subList.isEmpty())
          {
            subscription=subList.get(0);
          }


       }
       catch(Exception ex)
       {
          ex.printStackTrace();
       }

       return subscription;
   }
   public ArrayList<String> getSubscriberTopicsDetails(String subscriberID)
   {
       ArrayList<String> topics=new ArrayList<String>();
       Subscription subscription=null;
       ArrayList<Subscription> retrievedSubscriptions;

       try
       {
          Transaction tx=session.beginTransaction();
          Criteria criteria=session.createCriteria(Subscription.class).add(Restrictions.eq("subscriptionID",subscriberID));
          logger.info("Retrieving topics for subscriber: "+subscriberID);
          List<Subscription> subList=criteria.list();
          //int numberOfSubscriptions=subList.size();
          for(Subscription sub:subList)
          {
              if(topics.isEmpty())
              {
                topics.add(sub.getFilter());
                logger.info("Printing topics at listener: "+sub.getFilter());
              }
              else
              {
                 boolean topicAlreadyAdded=false;
                 for(String topic:topics)
                 {
                     if(topic.equals(sub.getFilter()))
                     {
                         topicAlreadyAdded=true;
                         break;
                     }
                 }
                 if(!topicAlreadyAdded)
                 {
                     topics.add(sub.getFilter());
                     logger.info("Printing topics at listener: "+sub.getFilter());
                 }
                 
              }

          }
          
       }
       catch(Exception ex)
       {
          ex.printStackTrace();
       }
       return topics;

   }
   public void deleteSubscription(Subscription subscription)
   {
        try
        {
            Transaction tx = session.beginTransaction ();
            session.delete (subscription);
            tx.commit ();
            logger.info ("Subscription deleted successfully");
           // return true;
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in deleting subscriber", ex);
           // return false;
        }
   }
}
